package com.pubinfo.passbook.api.controller.system;

import com.pubinfo.passbook.api.eip.UserInfoService;
import com.pubinfo.passbook.common.entity.TRabbitmqPersonConvert;
import com.pubinfo.passbook.common.entity.TSystemRole;
import com.pubinfo.passbook.common.exception.ServiceException;
import com.pubinfo.passbook.common.mapper.TRabbitmqPersonMapper;
import com.pubinfo.passbook.common.model.dto.role.RoleEditDTO;
import com.pubinfo.passbook.common.model.vo.base.JsonResult;
import com.pubinfo.passbook.common.model.vo.manage.RolePermissionVo;
import com.pubinfo.passbook.common.model.vo.system.UserRoleVo;
import com.pubinfo.passbook.common.service.master.TSystemRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

@RestController
@RequestMapping(value = "role/manager")
@Api(tags = "角色管理")
@Slf4j
public class RoleController {
    @Resource
    private TSystemRoleService roleService;
    @Resource
    private UserInfoService infoService;
    @Autowired
    TRabbitmqPersonMapper personMapper;

    @PostMapping("save")
    @ApiOperation("新增角色")
    //@JudgePermission(permission = "passbook_role_menu")
    public JsonResult<String> saveRole(@RequestBody RoleEditDTO dto) {
        return roleService.addRole(dto);
    }

    @GetMapping("findShengGongSiDepar")
    @ApiOperation("查询省公司部门列表")
    public JsonResult<List<String>> findShengGongSiDepar() {
        return JsonResult.success(personMapper.findShengGongSiDepar());
    }

    @DeleteMapping("remove/{roleId}")
    @ApiOperation("删除角色")
    public JsonResult<Boolean> removeRole(@PathVariable("roleId") Integer roleId) {
        return JsonResult.success(roleService.delRole(roleId));
    }

    @PostMapping("list")
    @ApiOperation("查询角色列表")
    public JsonResult<List<TSystemRole>> listRoles() {
        return JsonResult.success(roleService.list());
    }
    
    @PostMapping("currentUserRole")
    @ApiOperation("查询当前用户角色列表")
    public JsonResult<List<UserRoleVo>> currentUserRole() {
        List<UserRoleVo> voList = infoService.listCurrentUserRole();
        voList.sort(new Comparator<UserRoleVo>() {
            @Override
            public int compare(UserRoleVo o1, UserRoleVo o2) {
                return 0;
            }
        });
        return JsonResult.success(voList);
    }

    @PutMapping("switchRole/{roleId}")
    @ApiOperation("切换当前用户角色")
    public JsonResult<Boolean> switchRole(@PathVariable("roleId") String roleId) {
        return JsonResult.success(roleService.switchRole(roleId, infoService.getCurrentAcctId()));
    }
    
    @PutMapping("switchRoleByCurrUserRole")
    @ApiOperation("切换当前用户角色通过nsrsbh (当前用户角色列表)")
    public JsonResult<Boolean> switchRoleByCurrUserRole(String roleId, String nsrsbh, String orgId) {
        return JsonResult.success(infoService.setNsrsbhToCurrentUserInfo(roleId, nsrsbh, orgId));
    }
    
    @PutMapping("switchRoleByCurrUserRole/{roleId}")
    @ApiOperation("切换当前用户角色通过nsrsbh (当前用户角色列表)")
    public JsonResult<Boolean> switchRoleByCurrUserRole(@PathVariable("roleId") String roleId) {
        return JsonResult.success(infoService.setNsrsbhToCurrentUserInfo(roleId));
    }
    
    @PostMapping("getRolePermission")
    @ApiOperation("查询角色权限")
    public JsonResult<List<RolePermissionVo>> getRolePermission(Integer roleId) {
        return JsonResult.success(roleService.getRolePermission(roleId));
    }
    
    @PostMapping("addUserRole")
    @ApiOperation(("用户授权"))
    public JsonResult<Boolean> addUserRole(Integer acctId, Integer roleId, Integer orgId) {
        // Integer acctId = infoService.getCurrent4AUserInfo(token).getAcctId();
        return JsonResult.success(roleService.addUserRole(acctId, roleId, orgId));
    }

    @DeleteMapping("delUserRole")
    @ApiOperation(("移除用户权限"))
    public JsonResult<Boolean> delUserRole(Integer acctId) {
        return JsonResult.success(roleService.delUserRole(null, acctId));
    }

    @DeleteMapping("delPermRole")
    @ApiOperation(("移除角色权限"))
    public JsonResult<Boolean> delPermRole(Integer roleId) {
        return JsonResult.success(roleService.delRoleMenu(roleId));
    }

    @PostMapping("addPermRole")
    @ApiOperation(("角色授权"))
    public JsonResult<Boolean> addPermRole(Integer roleId, Integer[] permIds) {
        return JsonResult.success(roleService.addRoleMenu(roleId, Arrays.asList(permIds)));
    }

    @GetMapping("role_name_by_acct_id/{acctId}")
    @ApiOperation("获得用户角色名称")
    public JsonResult<String> getPersonRoleName(@PathVariable("acctId") String acctId) {
        return JsonResult.success(roleService.selectRoleNameByAcct(acctId));
    }

    @GetMapping("role_name_by_self")
    @ApiOperation("获得当前用户角色名称")
    public JsonResult<TSystemRole> getSelfRoleName() {
        return JsonResult.success(infoService.getCurrentUserRole());
    }


    @GetMapping("own")
    @ApiOperation("查询当前用户拥有的所有权限")
    public JsonResult<List<RolePermissionVo>> getCurrentUserOwnPerm(@RequestHeader("token") String token) {
    
        TRabbitmqPersonConvert user = infoService
            .getCurrent4AUserInfo(token);
    
        if (user == null) {
            throw new ServiceException("查无此人");
        }
        TSystemRole role = infoService.getCurrentUserRole();
        return JsonResult.success(roleService.getRolePermission(role.getId()));
    }
}
